<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:6}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+'-'+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:6}" :xl="{span:6}">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.sszym">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh +'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="招生年度">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsxn">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:12}" >
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="handleAdd" style="margin:0 10px">新增</a-button>
            <a-button type="primary" @click="loadData" >查询</a-button>
            <!-- <a-button type="primary" @click="handleExport">导出</a-button> -->
            <a-button type="primary" @click="handleBatchDelete" style="margin-left: 8px">批量删除</a-button>
             <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    </a-card>
    <a-card class="card" :bordered="false">
    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :pagination="pagination"
	   @change="handleTableChange"
    >
    <template slot="action" slot-scope="text,record">
        <a @click="handleEdit(record.id)">修改</a>
        <a @click="handleDelete(record.id)">删除</a>
      </template>
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>

      <!-- 审核 -->
      <rules-add :rulesAddVisible="rulesAddVisible" @close="handleAddClose" @success="handleAddSuccess" ></rules-add>
      <!-- 修改 -->
      <rules-edit :info="rulesEditData" :rulesEditVisible="rulesEditVisible" @close="handleEditClose" @success="handleEditSuccess" ></rules-edit>

    </a-card>
</div>
</template>

<script>
// import moment from 'moment'
import rulesAdd from './rulesAdd'
import rulesEdit from './rulesEdit'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

export default {
  components: {
    STable,
    rulesAdd,
    rulesEdit
  },
  data () {
    return {
      rulesAddVisible: false,
      rulesEditVisible: false,
      loading: false,
      data: [],
      advanced:true,
      // 查询参数
      queryParam: {
        ssyxm: '',
        sszym:'',
        zsxn:''
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '院系',
          dataIndex: 'ssyxm'
        },
        {
          title: '专业',
          dataIndex: 'sszym'
        },
        {
          title: '复试人数',
          dataIndex: 'fsrs'
        },
        {
          title: '学习形式',
          dataIndex: 'qrzhfqrz'
        },
        {
          title: '年度',
          dataIndex: 'zsxn'
        },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectedRowKeys:[],
      selectedRows:[],
      // custom table alert & rowSelection
      options: {
        alert: { show: false },
        rowSelection: {
          onChange: (selectedRowKeys, selectedRows) => {
            console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
          },
          onSelect: (record, selected, selectedRows) => {
            console.log(record, selected, selectedRows)
          },
          onSelectAll: (selected, selectedRows, changeRows) => {
            console.log(selected, selectedRows, changeRows)
          }
        }
      },
      selectMenu: {
        yx: [] ,// 院系
        zy:[]
      },
      rulesEditData: {},
      pagination:{
        current:1
      },
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
     // this.$set(this.queryParam,'ssyxm',this.selectMenu.yx[0].dwh)
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.ssyxm = res[0].data[0].dwh
        this.getProssion(this.queryParam.ssyxm)
         // 获取当前学年
        this.$api.base.getSchoolYear().then(res=>{
          this.queryParam.zsxn = res.data.masterAdmissionYear
           this.loadData({
            pageSize: 10,
            pageNum: 1
          })
        })
      })
    },
       // 多选
    onSelectChange (selectedRowKeys,selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
    // 选择院系
     yxChange (code) {
       this.$set(this.queryParam, 'sszym', '')
       this.$set(this.selectMenu, 'zy', [])
       if(code){
        this.getProssion(code)
       }
      },
      getProssion(code){
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
       // 加载数据
     loadData (parameter = {}) {
       if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
       let params = Object.assign(this.queryParam, parameter)
        this.loading = true
         this.$api.recruitstat.reExamProRuleSetList(params).then(res=>{
             const pagination = { ...this.pagination
            };
            pagination.total = 0
            if (res.data && res.data.total) {
              pagination.total = res.data.total;
            }
            this.data = []
            this.pagination = pagination;
            if (res.data && res.data.rows.length) {
              this.data = res.data.rows;
            }
            this.loading = false;
         })
     },
     // 分页
     handleTableChange (pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
     },
    // 导出
    handleExport () {
      console.log('导出')
      this.$export('admissions/ssfsfaxx/export',this.queryParam)
    },
    // 添加
    handleAdd () {
      this.rulesAddVisible = true
    },
    // 关闭添加
    handleAddClose () {
      this.rulesAddVisible = false
    },
    // 添加成功返回
    handleAddSuccess () {
      this.rulesAddVisible = false
      this.$message.success("添加成功")
      this.loadData({
        pageSize: 10,
        pageNum: 1
      });
    },
    // 修改
    handleEdit (code) {
      this.$api.recruitstat.reExamProRuleSetId(code).then( res => {
        this.rulesEditVisible = true
        this.rulesEditData = res.data
      })
    },
    // 关闭修改
    handleEditClose () {
      this.rulesEditVisible = false
    },
    // 修改成功返回
    handleEditSuccess () {
      this.rulesEditVisible = false
      this.$message.success("修改成功")
      this.loadData({
        pageSize: 10,
        pageNum: this.pagination.current
      });
    },
    // 删除
    handleDelete (code) {
       var that = this;
      this.$confirm({
        title: '确定删除?',
        onOk() {
          console.log('OK');
          that.deleteData(code)
        },
        onCancel() {
          console.log('Cancel');
        },
        class: 'test',
      });
    },
    // 删除
    handleBatchDelete () {
      console.log('批量删除')
      var deleteId = ''
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的数据')
        return
      }
      if(this.selectedRows && this.selectedRows.length){
        deleteId = this.selectedRows.map(v => {
          return v.id
        }).toString()
      }
      console.log(deleteId)
         var that = this;
        this.$confirm({
          title: '确定批量删除?',
          onOk() {
            console.log('OK');
            that.deleteData(deleteId)
          },
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
    },
    deleteData(code){
      this.$api.recruitstat.reExamProRuleSetDelete(code).then( res => {
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        });
        this.selectedRowKeys = []
      })
    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
